package controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.activiti.engine.task.Task;

import wfm.activiti.ActivitiManager;
import wfm.entities.Ticket;
import wfm.entities.User;
import wfm.entities.WFInfo;
import wfm.query.QueryDao;

import com.web.util.WebUtils;


@ManagedBean(name="endProtocolController")
@SessionScoped
public class EndProtocolController {

	@ManagedProperty(value = "#{wfInfo}")
	private WFInfo wfInfo;
	
	@ManagedProperty(value="#{user}")
	private User user;
	
	@ManagedProperty(value="#{notes}")
	private String notes;

	private Ticket ticket;
	
	@ManagedProperty(value="#{actualCosts}")
	private String actualCosts="0"; 
	
	@ManagedProperty(value="#{actualRepairDuration}")
	private String actualRepairDuration="0"; 
	
	public EndProtocolController(){
		
	}
	
	@PostConstruct
	public void init() {
		ticket=(Ticket)QueryDao.getVariableToProcessById(wfInfo.getInstanceId(), "ticket");
	}
	
	public Ticket getTicket() {
		return ticket;
	}
	public void setTicket(Ticket ticket) {
		this.ticket = ticket;
	} 
	
	public String finish(){
		String nextPage = "myTasks";
		
		ticket.setProtocolEndNotes(this.notes);
		ticket.setActualCosts(Double.parseDouble(this.actualCosts));
		ticket.setActualRepairDuration(Double.parseDouble(this.actualRepairDuration));
		QueryDao.saveVariableToProcessById(wfInfo.getInstanceId(), "ticket",
				ticket);

		// Retrieving form fields
		Map<String, String> prop = new HashMap<String, String>();
		prop.put("accountNr", "2322232");
		prop.put("amount", ""+ticket.getActualCosts());
		
		// Retrieving Activiti engine
		ActivitiManager manager = ActivitiManager.getInstance();	

		try {
			// Submitting task form -> go to next Activity
			String taskId = wfInfo.getTaskId();
			manager.getProcessEngine().getFormService().submitTaskFormData(taskId, prop);

		} catch (Throwable t) {
			 t.printStackTrace();
			FacesContext fc = FacesContext.getCurrentInstance();
			FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,"Problem with Webservice!!!", null);
			fc.addMessage(null, msg);
			fc.renderResponse();

			return "";
		}

		// Check for any execution errors
		String instanceId = wfInfo.getInstanceId();

		Task task = QueryDao.getNextTaskAndSetAssignee(instanceId, this.user);

		if (task != null) {
			wfInfo.setTaskId(task.getId());
			nextPage = QueryDao.getFormKey(wfInfo.getTaskId());
		}

		return WebUtils.redirect(nextPage);
	}

	public WFInfo getWfInfo() {
		return wfInfo;
	}

	public void setWfInfo(WFInfo wfInfo) {
		this.wfInfo = wfInfo;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getNotes() {
		return notes;
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}
	
	public String getActualCosts() {
		return actualCosts;
	}

	public void setActualCosts(String actualCosts) {
		this.actualCosts = actualCosts;
	}

	public String getActualRepairDuration() {
		return actualRepairDuration;
	}

	public void setActualRepairDuration(String actualRepairDuration) {
		this.actualRepairDuration = actualRepairDuration;
	}
	
}
